package com.beeantec.jnrerc.modules.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.BaseIdReqDTO;
import com.beeantec.jnrerc.core.api.dto.BaseIdsReqDTO;
import com.beeantec.jnrerc.core.api.dto.BaseStateReqDTO;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;

import com.beeantec.jnrerc.core.utils.BeanMapper;
import com.beeantec.jnrerc.dto.ExamDTO;
import com.beeantec.jnrerc.dto.ExamRepoDTO;
import com.beeantec.jnrerc.dto.request.ExamListReqDTO;
import com.beeantec.jnrerc.dto.request.ExamSaveReqDTO;
import com.beeantec.jnrerc.dto.response.ExamAdminDetailDTO;
import com.beeantec.jnrerc.dto.response.ExamOnlineRespDTO;
import com.beeantec.jnrerc.dto.response.ExamRespDTO;
import com.beeantec.jnrerc.dto.response.ExamReviewRespDTO;
import com.beeantec.jnrerc.entity.Course;
import com.beeantec.jnrerc.entity.Exam;

import com.beeantec.jnrerc.entity.ExamRepo;
import com.beeantec.jnrerc.modules.depart.service.DepartmentService;
import com.beeantec.jnrerc.modules.exam.service.ExamDepartService;
import com.beeantec.jnrerc.modules.exam.service.ExamRepoService;
import com.beeantec.jnrerc.modules.exam.service.ExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* <p>
* 考试控制器
* </p>
*
* @author Anthony
* @since 2020-07-25 16:18
*/
@Api(tags={"考试"})
@RestController
@RequestMapping("/api/exam")
public class ExamController extends BaseController {

    @Autowired
    private ExamService baseService;

    @Autowired
    private ExamRepoService examRepoService;

    @Autowired
    private ExamDepartService examDepartService;

    @Autowired
    private DepartmentService departmentService;


    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody ExamSaveReqDTO reqDTO) {

        //复制参数
        reqDTO.setCreateTime(new Date());
        baseService.save(reqDTO);
        return super.success();
    }




    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest edit(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param examId
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.GET})
    public ApiRest<Exam> find(@RequestParam String examId) {
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Exam::getId,examId);
        Exam dto = baseService.getOne(queryWrapper);
        return super.success(dto);
    }




    /**
     * 查找详情
     * @param examId
     * @return
     */
    @ApiOperation(value = "APP查找详情")
    @RequestMapping(value = "/app/detail", method = { RequestMethod.GET})
    public ApiRest<ExamRespDTO> findExamDetail(@RequestParam String examId) {
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Exam::getId,examId);
        Exam dto = baseService.getOne(queryWrapper);
        ExamRespDTO examRespDTO = new ExamRespDTO();
        examRespDTO.setExamDetail(dto);
        QueryWrapper<ExamRepo> repoQueryWrapper = new QueryWrapper<>();
        repoQueryWrapper.lambda().eq(ExamRepo::getExamId,examId);
        ExamRepo examRepo = examRepoService.getOne(repoQueryWrapper);
        examRespDTO.setRepoDetail(examRepo);
        // 考试部门
        List<String> departIds = examDepartService.listByExam(examId);
        if (!ObjectUtils.isEmpty(departIds)){
            examRespDTO.setDepartmentList(departmentService.listByIds(departIds));
        }
        return super.success(examRespDTO);
    }


    /**
     * 查找详情
     * @param examId
     * @return
     */
    @ApiOperation(value = "后端查询查找详情")
    @RequestMapping(value = "/admin/detail", method = { RequestMethod.GET})
    public ApiRest<ExamAdminDetailDTO> findDetail(@RequestParam String examId) {
        ExamAdminDetailDTO examAdminDetailDTO = new ExamAdminDetailDTO();
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Exam::getId,examId);
        Exam dto = baseService.getOne(queryWrapper);
        BeanUtils.copyProperties(dto,examAdminDetailDTO);
        QueryWrapper<ExamRepo> repoQueryWrapper = new QueryWrapper<>();
        repoQueryWrapper.lambda().eq(ExamRepo::getExamId,examId);
        List<ExamRepo> examRepos = new ArrayList<>();
        examRepos = examRepoService.list(repoQueryWrapper);
        examAdminDetailDTO.setRepoList(BeanMapper.mapList(examRepos, ExamRepoDTO.class));
        // 考试部门
        List<String> departIds = examDepartService.listByExam(examId);
        if (!ObjectUtils.isEmpty(departIds)){
            examAdminDetailDTO.setDepartmentList(departmentService.listByIds(departIds));
        }
        return super.success(examAdminDetailDTO);
    }


    /**
     * 查找详情
     * @param courseId
     * @return
     */
    @ApiOperation(value = "获取课程相关考试")
    @RequestMapping(value = "/detailByCourse", method = { RequestMethod.GET})
    public ApiRest<Exam> find(@RequestParam Long courseId) {
        QueryWrapper<Exam> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(Exam::getCId,courseId);
        Exam dto = baseService.getOne(queryWrapper);
        return super.success(dto);
    }

    /**
     * 查找详情
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "更新考试")
    @RequestMapping(value = "/state", method = { RequestMethod.POST})
    public ApiRest state(@RequestBody BaseStateReqDTO reqDTO) {

        QueryWrapper<Exam> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(Exam::getId, reqDTO.getIds());
        Exam exam = new Exam();
        exam.setState(reqDTO.getState());
        exam.setUpdateTime(new Date());

        baseService.update(exam, wrapper);
        return super.success();
    }


    /**
     * 分页查找
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "考试视角")
    @RequestMapping(value = "/online-paging", method = { RequestMethod.POST})
    public ApiRest<IPage<ExamOnlineRespDTO>> myPaging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {

        //分页查询并转换
        IPage<ExamOnlineRespDTO> page = baseService.onlinePaging(reqDTO);
        return super.success(page);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<Exam>> paging(@RequestBody ExamListReqDTO reqDTO) {
        //分页查询并转换
        QueryWrapper<Exam> examQueryWrapper = new QueryWrapper<>();
        Page<Exam> page = new Page<>(reqDTO.getPage(), reqDTO.getSize());
        IPage<Exam> examIPage = baseService.getBaseMapper().selectPage(page,examQueryWrapper);
        return super.success(examIPage);
    }


//    /**
//     * 分页查找
//     * @param reqDTO
//     * @return
//     */
//    @ApiOperation(value = "根据考试查看相关部门")
//    @RequestMapping(value = "/review-paging", method = { RequestMethod.POST})
//    public ApiRest<String> reviewPaging(@RequestBody PagingReqDTO<ExamDTO> reqDTO) {
//
//        //分页查询并转换
//        IPage<ExamReviewRespDTO> page = baseService.reviewPaging(reqDTO);
//
//        return super.success(page);
//    }


}
